/**
 * @version 1.0
 * @created 02 December 2010
 *
 * CheckoutServiceImplementor
 *
 * Copyright @ 2010 by CES
 * All Rights Reserved.
 * CES CONFIDENTIAL AND TRADE SECRET
 * 
 */
package com.ces.knacky.services.implementor;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.ces.knacky.framework.exception.DataAccessException;
import com.ces.knacky.framework.model.IEntity;
import com.ces.knacky.framework.services.implementor.CommonServiceImplementor;
import com.ces.knacky.model.Flag;
import com.ces.knacky.model.Item;
import com.ces.knacky.model.OrderItem;
import com.ces.knacky.model.OrderStatus;
import com.ces.knacky.model.OrderTable;
import com.ces.knacky.model.UserTable;
import com.ces.knacky.services.CheckoutService;
import com.ces.knacky.services.InventoryService;
import com.ces.knacky.services.implementor.InventoryServiceImplementor.InventoryBean;
/**
 * It is used to process order which arrives from both direct payment and auction work flow.
 * 
 * @author bipul_sinha
 *
 */
public class CheckoutServiceImplementor extends CommonServiceImplementor implements CheckoutService {
	
	@Autowired
	private InventoryService inventoryService;
	
	/**
	 * It is used to place order for the selected items whose ids are given.
	 *  
	 * @param 	itemIds
	 * 			Represents persistent ids of Item table for whom the order needs to be placed.
	 * @return	Integer
	 * 			Persistent Id of the object after save operation.
	 */
	public Integer placeOrder(String itemIds) {
		if (itemIds != null) {
			Set <String> selectedItems = new HashSet<String>(Arrays.asList(itemIds.split(",")));
			if(!selectedItems.isEmpty()) {
				String cartString = selectedItems.toString();
				String ids = cartString.substring(1, cartString.length()-1);
				List<Item> itemList = (List<Item>) list("SELECT item FROM Item item where item.id IN (" + ids + ")");
	
				OrderTable order = new OrderTable();
				order.setCreateTime(new Date());
				order.setUpdateTime(new Date());
				IEntity entity = findFirstByDescription(UserTable.class, "admin");
				order.setUser((UserTable)entity);
				IEntity flagEntity = findFirstByDescription(Flag.class, "ACTIVE");
				order.setFlag((Flag)flagEntity);
				entity =  findFirstByDescription(OrderStatus.class, "Pending");
				order.setOrderStatus((OrderStatus) entity);
				
				OrderItem orderItem = null;			
				Set <OrderItem> orderItems = new HashSet<OrderItem>();
				for (Item item : itemList) {
					orderItem = new OrderItem();
					orderItem.setOrder(order);
					orderItem.setItem(item);
					orderItem.setQuantity(1);
					orderItem.setFlag((Flag) flagEntity);
					orderItems.add(orderItem);
				}
				order.setOrderItems(orderItems);
				return save(order);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	/**
	 * It is used to fetch list of items whose order has been placed by the user.
	 *   
	 * @param 	username
	 * 			Represents name of the user who has placed order.
	 * @param 	orderId
	 * 			Order persistent identifier whose order item needs to be fetched.
	 * @return	List<IEntity>
	 * 			Returns list of items whose order is placed.
	 */
	public List<? extends IEntity> fetchPlacedOrder(String username, Integer orderId) {
		String query = 	"SELECT orderItem from OrderItem orderItem WHERE " +
						"orderItem.order.user.description='" + username +  
						"' AND orderItem.order.id = " + orderId;
		return list(query);		
	}	
	
	/**
	 * This methods checks the availability of the order items.
	 * @param orderId - persistent id of the order for which item availability is to be checked.
	 * @return - true , if items are available ,false otherwise
	 * @throws DataAccessException 
	 */
	@Override
	public boolean isOrderItemAvailable(int orderId) throws DataAccessException {
		List<InventoryBean> inventoryBeanList = getInventoryService().checkAvailability(orderId);
		for(InventoryBean inventoryBean : inventoryBeanList){
			System.out.println("inventoryBean.getItemId() = "+ inventoryBean.getItemId());
			System.out.println("inventoryBean..getAvailableQuantity() = "+ inventoryBean.getAvailableQuantity());
			System.out.println("inventoryBean.getRequestedQuantity() = "+ inventoryBean.getRequestedQuantity());			
		}
		return false;
	}
	
	public InventoryService getInventoryService() {
		return inventoryService;
	}
	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}
	
}
